package com.nt.train.processor;
import com.nt.train.beanlife.MyBeanLifeService;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Component;
/**
 * 如果后处理器没有以组件的方式加装到ioc容器中，处理器将不会发生作用
 * https://www.cnblogs.com/hurro/p/13549295.html
 * @author hlf
 * @version 1.0
 * @date 2022/9/28 21:33
 */
//@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor  {

    /**
     * 在调用bean的构造方法实例化bean之前，都会先调用这个方法
     * @param beanClass
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println( beanName + "实例化之前 调用 postProcessBeforeInstantiation（）");
        return null;
    }


    /**
     * bean实例化完成之后，会调用这个方法
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (bean instanceof  MyBeanLifeService){
            MyBeanLifeService myBeanLifeService = (MyBeanLifeService) bean;
            System.out.println("A实例对象此时的属性 name = " + myBeanLifeService.getName());
        }
        System.out.println( beanName+ "对象实例化完成 调用postProcessAfterInstantiation 对象类型为：" + bean.getClass().getName() );
        return false;
    }
}
